package com.leather.quan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.leather.common.entity.Quan;
import com.leather.common.entity.QuanImg;
import com.leather.quan.vo.QuanQueryVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author kevin
 */
public interface QuanMapper extends BaseMapper<Quan> {


    /**
     * 首页圈子动态
     * @param pageId
     * @return
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "quan_desc", property = "quanDesc"),
            @Result(column = "quan_img_url", property = "quanImgUrl"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "comments", property = "comments"),
            @Result(property = "images", column = "id",many = @Many(select = "com.leather.quan.mapper.QuanMapper.getQuanImgs"))
    })
    @Select("select a.*,b.nick_name,b.img_url from quan a,`user` b where a.user_id = b.id order by a.create_time desc limit #{pageId},20")
    List<QuanQueryVo> indexQuery(@Param("pageId") int pageId);


    /**
     * 根据用户id获取List
     * @param pageId
     * @param userId
     * @return
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "quan_desc", property = "quanDesc"),
            @Result(column = "quan_img_url", property = "quanImgUrl"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "comments", property = "comments"),
            @Result(property = "images", column = "id",many = @Many(select = "com.leather.quan.mapper.QuanMapper.getQuanImgs"))
    })
    @Select("select a.*,b.nick_name,b.img_url from quan a,`user` b where a.user_id = b.id and a.user_id = #{userId} order by a.create_time desc limit #{pageId},20")
    List<QuanQueryVo> getQuanByUserId(@Param("pageId") int pageId, @Param("userId") int userId);




    /**
     * 根据用户id获取List
     * @param searchValue
     * @return
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "quan_desc", property = "quanDesc"),
            @Result(column = "quan_img_url", property = "quanImgUrl"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "comments", property = "comments"),
            @Result(property = "images", column = "id",many = @Many(select = "com.leather.quan.mapper.QuanMapper.getQuanImgs"))
    })
    @Select("select a.*,b.nick_name,b.img_url from quan a,`user` b where a.user_id = b.id and a.quan_desc like concat('%',#{searchValue},'%') order by a.create_time desc")
    List<QuanQueryVo> searchValue( @Param("searchValue") String searchValue);


    /**
     * 根据quan_id获取QuanQueryVo对象
     * @param quanId
     * @return
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "quan_desc", property = "quanDesc"),
            @Result(column = "quan_img_url", property = "quanImgUrl"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(column = "img_url", property = "imgUrl"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "stars", property = "stars"),
            @Result(column = "comments", property = "comments"),
            @Result(property = "images", column = "id",many = @Many(select = "com.leather.quan.mapper.QuanMapper.getQuanImgs"))
    })
    @Select("select a.*,b.nick_name,b.img_url from quan a,`user` b where a.user_id = b.id and a.id = #{quanId}")
    QuanQueryVo getQuanById( @Param("quanId") int quanId);






    @Select("select * from quan_img where quan_id = #{qianId}")
    List<QuanImg> getQuanImgs(@Param("quanId") int quanId);

    @Update("update quan set stars = stars +1,update_time = now() where id = #{quanId}")
    int addStars(@Param("quanId") int quanId);

    @Update("update quan set comments = comments +1,update_time = now() where id = #{quanId}")
    int addComments(@Param("quanId") int quanId);

    @Update("update quan set comments = comments -1,update_time = now() where id = #{quanId}")
    int removeComments(@Param("quanId") int quanId);



}